<?php
/**
 * 树型下拉菜单
 * User: crstu
 * Date: 2016/5/28
 * Time: 23:41
 */

namespace common\widgets;


use yii\helpers\Html;
use yii\widgets\InputWidget;

class SelectTree extends InputWidget
{

    //主键字段名
    public $columnId = 'id';

    //父ID字段名
    public $columnPid = 'pid';

    //值字段名
    public $columnName = 'name';

    //初始化值
    public $initValue = false;

    public $isDisabled = false;

    public $data = [];


    public function run()
    {
        parent::run();
        $this->renderWidget();
    }


    public function renderWidget()
    {

        $tree = $this->genTree($this->data);

        if ($this->hasModel()) {
            $this->name = Html::getInputName($this->model, $this->attribute);
            $this->value = Html::getAttributeValue($this->model, $this->attribute);
        }

        echo '<select class="form-control" name="' . $this->name . '">';
        if (!empty($this->initValue)) {
            echo '<option value="0">' . $this->initValue . '</option>';
        }
        $list = $this->treeToSelect($tree);


        foreach ($list as $v) {
            $disabled = '';
            $selected = '';
            if ($this->isDisabled) {
                if (isset($v['disabled']) && !empty($v['disabled']))
                    $disabled = 'disabled="disabled"';
            }

            if ($this->value == $v['id'])
                $selected = 'selected="selected"';

            echo '<option value="' . $v['id'] . '" ' . $disabled . ' ' . $selected . '>' . str_repeat('&nbsp; ', $v['level'] * 3) . $v['value'] . '</option>';
        }
        echo '</select>';
    }


    public function treeToSelect($tree, $output = [], $level = 0)
    {
        foreach ($tree as $k => $v) {
            if (isset($v['son'])) {
                $output[] = ['id' => $v[$this->columnId], 'value' => $v[$this->columnName], 'level' => $level, 'disabled' => 1];
                $output = $this->treeToSelect($v['son'], $output, $level + 1);
            } else {
                $output[] = ['id' => $v[$this->columnId], 'value' => $v[$this->columnName], 'level' => $level];
            }
        }
        return $output;
    }

    /**
     * 暂时不用
     * @param $tree
     * @param int $level
     * @return array
     */
    public function treeToSelect2($tree, $level = 0)
    {
        $prefix = '';
        for ($i = 0; $i < $level; $i++) {
            $prefix .= '&nbsp;&nbsp;';
        }
        $level++;
        $output = [];
        foreach ($tree as $k => $v) {
            if (isset($v['son'])) {
                $output[$v[$this->columnName]] = $this->treeToSelect2($v['son'], $level);
            } else {
                $output[$v[$this->columnId]] = $prefix . $v[$this->columnName];
            }
        }
        return $output;
    }


    public function genTree($items)
    {

        $tree = array(); //格式化的树
        $tmpMap = array();  //临时扁平数据

        foreach ($items as $item) {
            $tmpMap[$item[$this->columnId]] = $item;
        }

        foreach ($items as $item) {
            if (isset($tmpMap[$item[$this->columnPid]])) {
                $tmpMap[$item[$this->columnPid]]['son'][$item[$this->columnId]] = &$tmpMap[$item[$this->columnId]];
            } else {
                $tree[$item[$this->columnId]] = &$tmpMap[$item[$this->columnId]];
            }
        }

        return $tree;
    }
}